
layui.use('element', function () {
    var element = layui.element;
});
layui.use(['flow','laytpl','jquery','layer'], function () {
    let flow = layui.flow
        ,laytpl = layui.laytpl
        ,$ = layui.jquery
        ,layer = layui.layer;

    let length=8;
    flow.load({     //流加载页面
        elem: '#goods-item'
        ,done: function (page, next) {
            //console.log(page);
            let lis = [];
            $.ajax({
                type: "get",
                url: "/getGoods",
                data: {
                    "page":page     //当前页数
                },
                success:function (res) {
                    layui.each(res.data,function (index,item) {
                        if(item.goodsCount===0){
                            return;
                        }
                        lis.push("<div class=\"layui-col-md3 layui-col-sm6\">\n" +
                            "\t\t\t\t\t\t\t<!-- Product item -->\n" +
                            "\t\t\t\t\t\t\t<div class='product-item'>\n" +
                            "\t\t\t\t\t\t\t\t<a>\n" +
                            "\t\t\t\t\t\t\t\t\t<img src='"+item.goodsImage+"' alt=\"image\">\n" +
                            "\t\t\t\t\t\t\t\t</a>\n" +
                            "\t\t\t\t\t\t\t\t<div class=\"product-caption\">\n" +
                            "\t\t\t\t\t\t\t\t\t<h4 class=\"product-name\">\n" +
                            "\t\t\t\t\t\t\t\t\t\t<span>"+item.goodsName+"</span>\n" +
                            "\t\t\t\t\t\t\t\t\t</h4>" +
                            "\t\t\t\t\t\t\t\t\t<div class=\"product-price-group\">\n" +
                            "\t\t\t\t\t\t\t\t\t\t<span class=\"product-count\" style='display: none' value='"+item.goodsCount+"'>剩余数量："+item.goodsCount+"</span>\n" +
                            "\t\t\t\t\t\t\t\t\t\t<span class=\"product-price\" price='"+item.goodsPrice+"' id='"+(page*8+1+index)+"'>"+item.goodsPrice+"<span class=\"font-small\">积分</span></span>\n" +
                            "<span class=\"product-count count\">剩余"+item.goodsCount+"件</span>"+
                            "\t\t\t\t\t\t\t\t\t</div>\n" +
                            "\t\t\t\t\t\t\t\t\t<div class=\"ht-btn-group\">\n" +
                            "\t\t\t\t\t\t\t\t\t\t<button class=\"exchange layui-btn layui-btn-fluid\" id='"+item.goodsId+"'>兑换</button>\n" +
                            "\t\t\t\t\t\t\t\t\t</div>\n" +
                            "\t\t\t\t\t\t\t\t</div>\n" +
                            "\t\t\t\t\t\t\t</div>\n" +
                            "\t\t\t\t\t\t</div>");
                    });
                    next(lis.join(''), page<res.pages);


                    length+=lis.length;     //记录有当前商品列表有多少个商品
                    //console.log("length="+length);
                        $(".exchange").on('click',function () {
                            layer.open({
                                title: '积分兑换'
                                ,content: '确定要兑换该物品吗？'
                                ,btn:['兑换','取消']
                                ,shadeClose:true
                                ,closeBtn:false
                                ,yes:function () {
                                    if(Number(userIntegral) < Number(price))   //验证用户积分是否足够
                                    {
                                        //console.log("积分不足");
                                        return layer.msg('积分不足兑换失败！',{icon:5})
                                    }else {
                                        $.ajax({
                                            type: 'post',
                                            url: '/goodsExchange',
                                            data: {
                                                "goodsId": goodsId
                                            },
                                            success: function (res) {
                                                if (res === 1) {
                                                    getIntegral();
                                                    layer.msg('兑换成功!', {icon: 6});
                                                    window.location.href = "/shop";
                                                } else {
                                                    layer.msg('兑换失败!', {icon: 5})
                                                }
                                            }

                                        })
                                    }

                                }
                            })
                            //console.log("当前点击："+i)
                        })

                },
                error:function (e) {

                }

            });
        }
    })


});

var $ = layui.$, active = {
    reload: function(){
        var demoReload = $('#demoReload');

        //执行重载
        table.reload('testReload', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                key: {
                    id: demoReload.val()
                }
            }
        }, 'data');
    }
};

let userIntegral;
function getIntegral() {
    $.ajax({
        type: 'get',
        url: 'getIntegral',
        success:function (result) {
            //console.log(result);
            userIntegral = result;
            $("#integral").text(result);
        }
    })
}
window.onload = function(){
    getIntegral();
};
let price;
let count;
let goodsId;


$("body").on('click','.exchange',function () {
    price = $(this).parent().prev().children().eq(1).attr("price");
    count = $(this).parent().prev().children().eq(0).text();

    goodsId = $(this).attr('id');
});

let province;
let city;
let county;